package demo01;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Class<?> studentClass = Class.forName("demo01.Student");
        Field[] declaredFields = studentClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            System.out.println(declaredField);
        }
        Student newInstance = (Student) studentClass.getDeclaredConstructor().newInstance();
        declaredFields[0].set(newInstance, "张三");
        declaredFields[1].set(newInstance, 18);
        System.out.println(newInstance.getName());
        System.out.println(newInstance.getAge());
        System.out.println("====================");
        Method[] declaredMethods = studentClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.toString());
        }
        // 修复：使用 getDeclaredConstructor().newInstance() 替代 newInstance()
        studentClass.getDeclaredMethod("show2", int.class).invoke(newInstance, 12);
    }
}
